package org.smartagents.body;

import org.smartagents.enviro.EnvironmentConstants;
import org.smartagents.global.Identifiable;
import org.smartagents.global.Trigerable;
import org.smartagents.global.Global;
import org.smartagents.util.Logger;


public class Substance extends Identifiable {

    private String name;


    private String[] speciality;

    private Body enclosing;

    @Override
    public void trigger(String impulse) {
        String[] splited = splitImpulse(impulse);
        if (speciality != null && splited != null) {
            if (isSatisfied(speciality[0], SubstanceConstants.MUSCLE) && isSatisfied(splited[0], SubstanceConstants.MUSCLE)) {
                Global.getInstance().getEnvironment().moveBody(enclosing, 1);
            } else if (isSatisfied(speciality[0], SubstanceConstants.ROTATOR) && isSatisfied(splited[0], SubstanceConstants.ROTATOR)) {
                int u =(1+(int)Math.abs(Math.random()*3));
                Global.getInstance().getEnvironment().rotateBody(enclosing, u);
            }
        } 

        if (enclosing != null) {
            enclosing.getCortex().trigger(impulse);
        }

    }

    private String[] splitImpulse(String impulse) {
        return impulse.split("\\" + EnvironmentConstants.IMPULSE_SEPARATOR);
    }

    @Override
    public void tick() {
        if (speciality != null) {
            if (isSatisfied(speciality[0], SubstanceConstants.LIGHT_SENSOR)) {
                String substance = Global.getInstance().getEnvironment().getNeighbors(enclosing, Integer.valueOf(speciality[1]), Integer.valueOf(speciality[2]));
                if (substance!=null) {
                    trigger(name + "|" + speciality[1] + "|" + substance);
                }
            } else if (isSatisfied(speciality[0], SubstanceConstants.COUNTER)) {
                trigger(speciality[1]);
            }
            else if (isSatisfied(speciality[0], SubstanceConstants.RESET_TRIGGER)) {
                trigger(SubstanceConstants.RESET_TRIGGER);
            }
        }
    }

    public Body getEnclosing() {
        return enclosing;
    }

    public void setEnclosing(Body enclosing) {
        this.enclosing = enclosing;
    }

    public String[] getSpeciality() {
        return speciality;
    }

    public void setSpeciality(String[] speciality) {
        this.speciality = speciality;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String identify() {
        return name;
    }


    public String toString() {
        StringBuffer buf = new StringBuffer();

        buf.append(getName()).append(EnvironmentConstants.SUBSTANCE_DETAILS_SEPARATOR);
        buf.append(getMaxTick()).append(EnvironmentConstants.SUBSTANCE_DETAILS_SEPARATOR);
        buf.append(getLinkCondition()).append(EnvironmentConstants.SUBSTANCE_DETAILS_SEPARATOR);
        int i = 0;
        for (String str : getSpeciality()) {
            if (i++ != 0) {
                buf.append(EnvironmentConstants.SUBSTANCE_SPECIALITY_SEPARATOR);
            }
            buf.append(str);
        }

        return buf.toString();
    }
}
